package com.citusdata.elven.httpRpc;


public class HttpRpcUrl {
  protected static final String METHOD = "POST";
  protected static final String URL_SEPARATOR = "/";
  public static final String SHUTDOWN_URL = "/shutdown";

  private final String  serviceName;
  private final String  methodName;
  private final String  url;
  private final boolean valid;

  public HttpRpcUrl(String serviceName, String methodName) {
    this.serviceName = serviceName;
    this.methodName  = methodName;
    this.url = URL_SEPARATOR + serviceName + URL_SEPARATOR + methodName;
    this.valid = true; 
  }

  public HttpRpcUrl(String url) {
    this.url = url;
    
    // Parse the url, and check to see if it is valid. 
    int first  = url.indexOf(URL_SEPARATOR);
    int second = url.lastIndexOf(URL_SEPARATOR);

    if ( (0 <= first) && (first < second) && (second+1 < url.length()) ) {
      this.serviceName = url.substring(first+1, second);
      this.methodName  = url.substring(second+1);
      this.valid = true;
    } else {
      this.serviceName = null;
      this.methodName  = null;
      this.valid = false;
    }
  }

  public String getUrl() {
    return url;
  }

  public String getServiceName() {
    return serviceName;
  }

  public String getMethodName() {
    return methodName;
  }

  public boolean valid() {
    return valid;
  }
}
